package com.fengye.school.model.school;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fengye.school.json.SexTypeJsonDeserializer;
import com.fengye.school.json.SexTypeJsonSerializer;
import com.fengye.school.json.StudentTypeJsonDeserializer;
import com.fengye.school.json.StudentTypeJsonSerializer;
import com.fengye.school.model.BaseEntity;
import com.fengye.school.model.school.enums.SexType;
import com.fengye.school.model.school.enums.StudentType;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * 学生表
 */
@Table(name = "tb_s_student")
@org.hibernate.annotations.Table(appliesTo = "tb_s_student", comment = "专业表")
@Entity

@Getter
@Setter
public class Student extends BaseEntity {
    @Column(name = "name", columnDefinition = "varchar(100) COMMENT '学生姓名'")
    private String name;

    @Column(name = "english_name", columnDefinition = "varchar(100) COMMENT '学生英文姓名'")
    private String englishName;

    @Column(name = "code", columnDefinition = "varchar(100) COMMENT '学号'")
    private String code;

    @Column(name = "email", columnDefinition = "varchar(100) COMMENT '邮箱'")
    private String email;
    @Column(name = "enroll_time", columnDefinition = "datetime COMMENT '入学时间'")
    private Date enrollTime;
    @Column(name = "graduate_time", columnDefinition = "datetime COMMENT '毕业时间'")
    private Date graduateTime;
    @Column(name = "age", columnDefinition = "int COMMENT '年龄'")
    private Integer age;
    @Column(name = "sex", columnDefinition = "varchar(10) COMMENT '性别'")
    private SexType sex;

    @ManyToMany
    @JoinTable(name = "tb_s_student_course",
            joinColumns = {@JoinColumn(name = "student_id", columnDefinition = "bigint comment '学生id'")},
            inverseJoinColumns = {@JoinColumn(name = "course_id", columnDefinition = "bigint comment '课程id'")}
    )
    @JsonIgnore
    private Set<Lesson> lessonSet;


    @Column(name = "student_type", columnDefinition = "varchar(10) COMMENT '学生类型'")
    private StudentType studentType;

    /**
     * 学生所在班级
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "class_id", columnDefinition = "bigint comment '所在班级id'")
    @JsonIgnore
    private EClass eClass;

    @JsonSerialize(using = SexTypeJsonSerializer.class)
    public SexType getSex() {
        return sex;
    }

    @JsonDeserialize(using = SexTypeJsonDeserializer.class)
    public void setSex(SexType sexType) {
        this.sex = sexType;
    }

    @JsonSerialize(using = StudentTypeJsonSerializer.class)
    public StudentType getStudentType() {
        return studentType;
    }

    @JsonDeserialize(using = StudentTypeJsonDeserializer.class)
    public void setStudentType(StudentType studentType) {
        this.studentType = studentType;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", englishName='" + englishName + '\'' +
                ", code='" + code + '\'' +
                ", email='" + email + '\'' +
                ", enrollTime=" + enrollTime +
                ", graduateTime=" + graduateTime +
                ", age=" + age +
                ", sex=" + sex +
                ", create_time=" + this.getCreateTime() +
                ", update_time=" + this.getUpdateTime() +
                '}';
    }
}
